package com.earogya.logic.pharmacy.request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.struts.ehr.patient_details.model.PatientDetails;
import org.apache.struts.pharmacy.pharmacy.model.*;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.group.*;
import ca.uhn.hl7v2.model.v251.message.TBR_R08;
import ca.uhn.hl7v2.model.v251.segment.PID;

/***
 * Create a TBR_R08 HL7 query message receives Prescription Drug List object
 * prescription drug list to a patient.
 * @author klanerolle@gmail.com
 *
 */
public class TBR_R08_Handler {
	private TBR_R08 adtMsg;
	private List<DrugsGivenToPatient> prescriptionDrugsList = new ArrayList<DrugsGivenToPatient>();
	
	public TBR_R08_Handler(){
		
	}
	/**
	 * Accept a Prescription Drug List object.
	 * @param adtMsg
	 */	
	public TBR_R08_Handler(TBR_R08 adtMsg){
		this.adtMsg=adtMsg;
	}
	
	/**
	 * Add search Prescription Drug List details to segments RDT
	 * @throws HL7Exception
	 */
	public List<DrugsGivenToPatient> manageMessage() throws HL7Exception{
		
		for(int i=0; i <adtMsg.getRDTReps();i++){
			prescriptionDrugsList.add(new DrugsGivenToPatient());
			String sMessage = adtMsg.getRDT(i).getColumnValue().encode();
			String saMessage[] = sMessage.split(" ! ");
			DrugsGivenToPatient drugs = prescriptionDrugsList.get(i);
			drugs.setDispencedQuantity(saMessage[7]);
			drugs.setDosage(saMessage[8]);
			drugs.setDrugId(saMessage[3]);
			drugs.setDrugName(saMessage[2]);
			drugs.setPatientId(saMessage[1]);
			drugs.setPrescriptionNo(saMessage[0]);
			drugs.setMaxAmount(saMessage[5]);
			drugs.setMinAmount(saMessage[4]);
			drugs.setDispenceUnits(saMessage[6]);
			drugs.setRequestDate(saMessage[9]);			
		}
		return prescriptionDrugsList;
	}
}
